﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using MotionCS.Enumerate;

namespace MotionCS.CameraVision
{
    public enum CameraTypeE
    {
        /// <summary>
        /// 海康
        /// </summary>
        Hikvision,
        /// <summary>
        /// 大华
        /// </summary>
        Dahua
    }

    public enum CameraColorE
    {
        /// <summary>
        /// 彩色
        /// </summary>
        Rgb,
        /// <summary>
        /// 黑白
        /// </summary>
        Mono,
    }

    public struct CamInfo
    {
        /// <summary>
        /// 相机名称
        /// </summary>
        public string strName;
        /// <summary>
        /// 相机IP
        /// </summary>
        public string strIP;
        /// <summary>
        /// 相机SN
        /// </summary>
        public string strSN;
        /// <summary>
        /// 相机厂商
        /// </summary>
        public string strManuf;
        /// <summary>
        /// 相机型号
        /// </summary>
        public string strModelName;
    }

    public struct FrameInfo
    {
        /// <summary>
        /// 图像ID
        /// </summary>
        public string uuid;
        /// <summary>
        /// 图像二维码
        /// </summary>
        public string code;
        /// <summary>
        /// 图像序号
        /// </summary>
        public int index;
        /// <summary>
        /// 取像类型
        /// </summary>
        public GrabLightTypeE LightType;
        /// <summary>
        /// 是否黑图
        /// </summary>
        public bool IsBlackPatch;
        /// <summary>
        /// 光机投影下标
        /// </summary>
        public int DlpLocationIndex;
        /// <summary>
        /// 光机图像下标
        /// </summary>
        public int DlpImageIndex;
    }

    public interface ICamera
    {
        /// <summary>
        /// 日志回调
        /// </summary>
        Action<string> LogoutCallBack { get; set; }
        /// <summary>
        /// 图像回调
        /// </summary>
        Action<IFrameData> ImageCallBack { get; set; }
        /// <summary>
        /// 曝光回调
        /// </summary>
        Action<string> ExposureCallBack { get; set; }
        /// <summary>
        /// 注册图像抓拍结束回调
        /// </summary>
        Action<string> GrabEndCallback { get; set; }
        /// <summary>
        /// 注册帧重拍回调
        /// </summary>
        Action<string, int> RemakeCallback { get; set; }
        /// <summary>
        /// 连接状态
        /// </summary>
        /// <returns></returns>
        bool IsConnected { get; }
        /// <summary>
        /// 相机信息
        /// </summary>
        CamInfo Info { get; set; }
        /// <summary>
        /// 打开连接相机
        /// </summary>
        /// <returns></returns>
        bool OpenDevice();
        /// <summary>
        /// 关闭相机连接
        /// </summary>
        void CloseDevice();
        /// <summary>
        /// 设置图像宽度
        /// </summary>
        /// <param name="width"></param>
        int SetImageWidth(uint width);
        /// <summary>
        /// 设置图像高度
        /// </summary>
        /// <param name="height"></param>
        int SetImageHeight(uint height);
        /// <summary>
        /// 设置触发选择器
        /// </summary>
        /// <param name="selector">0-帧触发开始 1-行开始</param>
        void SetTriggerSelector(uint selector);
        /// <summary>
        /// 设置触发模式
        /// </summary>
        /// <param name="mode">false 触发关闭  true 触发打开</param>
        void SetTriggerMode(bool mode);
        /// <summary>
        /// 设置触发源
        /// </summary>
        /// <param name="source">0-线性0  1-软件触发 2-线性1</param>
        void SetTriggerSource(uint source);
        /// <summary>
        /// 设置触发极限
        /// </summary>
        /// <param name="activation">0-上升沿 1-下降沿 2-高电平 3-低电平 4-双边沿</param>
        void SetTriggerActivation(uint activation);
        /// <summary>
        /// 设置曝光时间
        /// </summary>
        /// <param name="time"></param>
        void SetExposureTime(float time);
        /// <summary>
        /// 获取曝光时间
        /// </summary>
        /// <param name="time"></param>
        void GetExposureTime(ref float time);
        /// <summary>
        /// 设置部分图像输出模式
        /// </summary>
        /// <param name="mode">0-ImagePending 1-PartialImageOutput 
        /// 2-PartialImageDiscard 3-PartialImageFilled</param>
        void SetPartialImageOutputMode(uint mode);
        /// <summary>
        /// 设置线性使能
        /// </summary>
        /// <param name="enable"></param>
        void SetAbnormalLineEnable(bool enable);
        /// <summary>
        /// 设置增益值
        /// </summary>
        /// <param name="gain"></param>
        void SetGainValue(float gain);
        /// <summary>
        /// 获取增益值
        /// </summary>
        /// <param name="gain"></param>
        void GetGainValue(ref float gain);
        /// <summary>
        /// 设置硬件触发信息
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="code"></param>
        /// <param name="index"></param>
        /// <param name="total"></param>
        void SetHardTriggerInfo(FrameInfo info);
        /// <summary>
        /// 清空拍照Info队列
        /// </summary>
        void ClearGrabInfo();
        /// <summary>
        /// 软件触发
        /// </summary>
        void SoftwareTrigger();
        /// <summary>
        /// 软件触发
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="code"></param>
        /// <param name="index"></param>
        void SoftwareTrigger(string uuid, string code, int index);
        /// <summary>
        /// 软件触发
        /// </summary>
        /// <param name="uuid"></param>
        /// <param name="qrcode"></param>
        /// <param name="index"></param>
        /// <param name="total"></param>
        /// <param name="isBlackPatch"></param>
        /// <param name="grabLightType"></param>
        /// <returns></returns>
        Int32 SoftwareTrigger(FrameInfo info);
        /// <summary>
        /// 开始抓取
        /// </summary>
        void StartGrab();
        /// <summary>
        /// 停止抓取
        /// </summary>
        void StopGrab();
        /// <summary>
        /// 释放相机
        /// </summary>
        void Dispose();
    }

    public interface IFrameData
    {
        /// <summary>
        /// 相机序列号
        /// </summary>
        string SerialNo { get; set; }
        /// <summary>
        /// 设备IP
        /// </summary>
        string DeviceIp { get; set; }
        /// <summary>
        /// 图像数据RAW
        /// </summary>
        byte[] RawData { get; }
        /// <summary>
        /// 图像数据长度
        /// </summary>
        int RawLen { get; }
        /// <summary>
        /// 图像数据指针
        /// </summary>
        IntPtr RawPtr { get; }
        /// <summary>
        /// 图像宽度
        /// </summary>
        int Width { get; }
        /// <summary>
        /// 图像高度
        /// </summary>
        int Height { get; }
        /// <summary>
        /// 图像所属工件ID
        /// </summary>
        string Uuid { get; set; }
        /// <summary>
        /// 图像所属二维码
        /// </summary>
        string QrCode { get; set; }
        /// <summary>
        /// 图像索引
        /// </summary>
        int Index { get; set; }
        /// <summary>
        /// 相机名称
        /// </summary>
        string CameraName { get; set; }
        /// <summary>
        /// 光源类型
        /// </summary>
        GrabLightTypeE LightType { get; set; }
        /// <summary>
        /// 光机位置下标
        /// </summary>
        int DlpLocationIndex { get; set; }
        /// <summary>
        /// 光机图像下标
        /// </summary>
        int DlpImageIndex { get; set; }
        /// <summary>
        /// 是否彩色
        /// </summary>
        bool IsColor { get; }
        /// <summary>
        /// 转bitmap
        /// </summary>
        /// <returns></returns>
        Bitmap ToBitmap();
    }
}
